/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if(root == nullptr)
            return ret;
        queue<TreeNode*> qu;
        qu.push(root);
        while(!qu.empty())
        {
            int sz = qu.size();
            vector<int> ans;
            while(sz--)
            {
                TreeNode* cur = qu.front();
                qu.pop();   
                ans.push_back(cur->val);
                if(cur->left) qu.push(cur->left);
                if(cur->right) qu.push(cur->right);
            }
            ret.push_back(ans);
        }
        return ret;
    }
};
